package com.nexgo.payment.trans.setting.network;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.trans.setting.SaveParameterActivity;
import com.nexgo.payment.trans.setting.SaveParameterPresenter;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.SwitchButton;
import com.nexgo.payment.widget.TitleBuilder;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 网络参数设置
 */
public class SetupNetworkActivity extends SaveParameterActivity {

    @BindView(R.id.tpdu)
    EditText tpdu;
    @BindView(R.id.server_ip)
    EditText serverIp;
    @BindView(R.id.server_port)
    EditText serverPort;
    @BindView(R.id.communication_timeout)
    EditText communicationTimeout;
    @BindView(R.id.enable_ssl)
    SwitchButton enableSsl;
    @BindView(R.id.dnsUrl)
    EditText dnsUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_network);
        ButterKnife.bind(this);
        new SaveParameterPresenter(this, Repository.getInstance());

        initTitle();
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setRightText(getString(R.string.action_save))
                .setTitleText(getString(R.string.network_parameter_settings))
                .build();
    }

    @Override
    protected HashMap<String, String> getDataBind() {
        HashMap<String, String> map = new HashMap<>();
        map.put(PreferencesConstants.TPDU, tpdu.getText().toString());
        map.put(PreferencesConstants.SERVER_IP, serverIp.getText().toString());
        map.put(PreferencesConstants.SERVER_PORT, serverPort.getText().toString());
        map.put(PreferencesConstants.COMMUNICATION_TIMEOUT, communicationTimeout.getText().toString());
        map.put(PreferencesConstants.ENABLE_SSL, enableSsl.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.DNS_URL, dnsUrl.getText().toString().trim());
        return map;
    }

    @Override
    public void showDetail() {
        tpdu.setText(mPresenter.getParamValue(PreferencesConstants.TPDU));
        serverIp.setText(mPresenter.getParamValue(PreferencesConstants.SERVER_IP));
        serverPort.setText(mPresenter.getParamValue(PreferencesConstants.SERVER_PORT));
        communicationTimeout.setText(mPresenter.getParamValue(PreferencesConstants.COMMUNICATION_TIMEOUT));
        enableSsl.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.ENABLE_SSL)));
        dnsUrl.setText(mPresenter.getParamValue(PreferencesConstants.DNS_URL));
    }

    @OnClick({R.id.title_iv_left, R.id.title_tv_right})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            case R.id.title_tv_right:
                if (mPresenter != null) {
                    mPresenter.saveParams(getDataBind());
                }
                break;
            default:
                break;
        }
    }

    public static Intent newInstance(Context context) {
        return new Intent(context, SetupNetworkActivity.class);
    }
}
